package com.zkingsoft.web.tag;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.matrix.core.tools.StringUtils;
import com.zkingsoft.web.bean.ProductAttrDef;
import com.zkingsoft.web.dao.ProductAttrDefDao;
import com.zkingsoft.web.tool.SystemConstance;
import com.zkingsoft.web.tool.Tools;
import com.zkingsoft.web.tool.WebUtil;

public class AttrTag extends SimpleTagSupport {

	/**
	 *属性编码
	 */
	private String code;

	/**
	 * 是否需要子属性
	 */
	private boolean needChild=false;
	/**
	 * 变量名称
	 */
	private String var;
 
	/**
	 * 获取产品属性
	 * 
	 * @author jiangyouyao
	 */
	public void doTag() throws JspException, IOException {
		if(StringUtils.isBlank(code)){
			this.getJspContext().setAttribute(var, new ProductAttrDef());
		}else{
			ProductAttrDefDao productAttrDefDao = WebUtil.getService(this.getJspContext(), SystemConstance.PRODUCT_ATTRDEFDAO);
			String companyId=Tools.getCompanyId();
			ProductAttrDef attr=productAttrDefDao.selectByCode(code,companyId);
			this.getJspContext().setAttribute(var, attr);
			if(needChild){
				ProductAttrDef attrQuery=new ProductAttrDef();
				attrQuery.setParentId(attr.getId());
				List<ProductAttrDef> list=productAttrDefDao.selectByModel(attrQuery);
				this.getJspContext().setAttribute("childAttr", list);
			}
		}
		
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	} 

	
	public boolean isNeedChild() {
		return needChild;
	}

	public void setNeedChild(boolean needChild) {
		this.needChild = needChild;
	}


	StringWriter sw = new StringWriter();

}